#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
using ll = long long;
const int maxsize = 5;
const int maxn = 5 + 1e5;

int n, m;
int asiz;
struct Node {
    ll t, l, r; // type, l, r
    bool operator<(const Node &x) const { return l > x.l; }
} a[maxn];

int main() {
    cin >> n >> m;
    priority_queue<Node> qe;
    for (int i = 1; i <= m; ++i) {
        Node tmp;
        cin >> tmp.t >> tmp.l >> tmp.r;
        qe.push(tmp);
    }
    a[++asiz] = qe.top(), qe.pop();
    while (!qe.empty()) {
        Node tmp = qe.top();
        qe.pop();
        if (tmp.t == a[asiz].t)
            a[asiz].r =max(a[asiz].r,tmp.r);
        else
            a[++asiz] = tmp;
    }
    if (a[1].l == 1 && a[1].t == 2) { // 第一段是 2，gg！
        cout << "0/21/0";
        return 0;
    }

    // for (int i = 1; i <= asiz; ++i) {
    //     printf("%d %d %d ed\n", a[i].t, a[i].l, a[i].r);
    // }

    ll ans = 0;
    ll last = 0, g = 0; // 上次剩余，欠债
    for (int i = 1; i <= asiz; i += 2) {
        ll len1 = a[i].r - a[i].l + 1 + last, len2 = a[i + 1].r - a[i + 1].l + 1;
        if (i + 1 > asiz) { // 以 0 结尾
            last = len1;
            break;
        }
        if (len1 == len2) {
            ans += len1 * 3 + len2 * 1, last = 0;
        } else if (len1 > len2) {
            ans += len2 * 3 + len2 * 1, last = len1 - len2;
        } else if (len1 < len2) {
            ans += len1 * 3 + len2 * 1, last = 0, g += len2 - len1;
        }
    }
    // printf("%d %d %d ", ans, g, last);
    ans += g * 3;
    if (g % 2 == 1) ans += 3;
    ans += last / 2 * 4;
    if (last % 2 == 1) {
        if (g % 2 == 1)
            ans += 1;
        else
            ans += 3;
    }
    cout << ans;
}